package gamePlay;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;
import org.newdawn.slick.openal.SoundStore;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Menu {

	boolean load = true;
	boolean disp = true;
	boolean input = true;

	boolean tmpLevel;

	int mouseX;
	int mouseY;

	Texture back;
	Texture head;
	Texture menuBar;
	Texture menuBarLoad;
	Texture menuBarNew;
	Texture menuBarIns;
	Texture menuBarOpt;
	Texture menuBarExt;

	Audio mouseoverSound;
	Audio clickSound;

	public void load() {

		try {
			// Versuchen das Bild als Texture zu laden
			back = TextureLoader.getTexture("PNG", new FileInputStream(
					"./DATA/MainMenu/Menu_bg.png"));
			head = TextureLoader.getTexture("PNG", new FileInputStream(
					"./DATA/MainMenu/Menu_head.png"));
			menuBar = TextureLoader.getTexture("PNG", new FileInputStream(
					"./DATA/MainMenu/Title_menu.png"));
			menuBarLoad = TextureLoader.getTexture("PNG", new FileInputStream(
					"./DATA/MainMenu/Title_menu_LoadGame.png"));
			menuBarNew = TextureLoader.getTexture("PNG", new FileInputStream(
					"./DATA/MainMenu/Title_menu_NewGame.png"));
			menuBarIns = TextureLoader.getTexture("PNG", new FileInputStream(
					"./DATA/MainMenu/Title_menu_Instructions.png"));
			menuBarOpt = TextureLoader.getTexture("PNG", new FileInputStream(
					"./DATA/MainMenu/Title_menu_Options.png"));
			menuBarExt = TextureLoader.getTexture("PNG", new FileInputStream(
					"./DATA/MainMenu/Title_menu_Exit.png"));

			mouseoverSound = AudioLoader.getAudio("WAV", new FileInputStream(
					"./DATA/Sounds/mouseover.wav"));
			clickSound = AudioLoader.getAudio("WAV", new FileInputStream(
					"./DATA/Sounds/mouseclick.wav"));

			// Immer beim laden einer Ressource aufpassen
			// Beispiel: Es kann sein, dass die Dateien nicht da sind.
		} catch (IOException e) {
			e.printStackTrace();
		}

		load = false;
	}

	public void disp() {

		Color.white.bind();
		back.bind();

		GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(0, 0);
		GL11.glVertex2f(0, 0);
		GL11.glTexCoord2f(1, 0);
		GL11.glVertex2f(1024, 0);
		GL11.glTexCoord2f(1, 1);
		GL11.glVertex2f(1024, 1024);
		GL11.glTexCoord2f(0, 1);
		GL11.glVertex2f(0, 1024);
		GL11.glEnd();

		Color.white.bind();
		head.bind();

		GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(0, 0);
		GL11.glVertex2f(0, 0);
		GL11.glTexCoord2f(1, 0);
		GL11.glVertex2f(1000, 0);
		GL11.glTexCoord2f(1, 1);
		GL11.glVertex2f(1000, 500);
		GL11.glTexCoord2f(0, 1);
		GL11.glVertex2f(0, 500);
		GL11.glEnd();
	}

	public void input() {

		while (Mouse.next()) {

			mouseX = Mouse.getEventX();
			mouseY = Mouse.getEventY();

		}

		if (mouseX > 320 && mouseX < 704 && mouseY > 0 && mouseY < 480) {

			mouseoverSound.playAsMusic(1.0f, 1.0f, false);

			if (mouseX > 320 && mouseX < 704 && mouseY > 384 && mouseY <= 480) {

				Color.white.bind();
				menuBarLoad.bind();

				GL11.glBegin(GL11.GL_QUADS);
				GL11.glTexCoord2f(0, 0);
				GL11.glVertex2f(0, 0);
				GL11.glTexCoord2f(1, 0);
				GL11.glVertex2f(1000, 0);
				GL11.glTexCoord2f(1, 1);
				GL11.glVertex2f(1000, 1000);
				GL11.glTexCoord2f(0, 1);
				GL11.glVertex2f(0, 1000);
				GL11.glEnd();
			}

			if (mouseX > 320 && mouseX < 704 && mouseY > 288 && mouseY <= 384) {

				Color.white.bind();
				menuBarNew.bind();

				GL11.glBegin(GL11.GL_QUADS);
				GL11.glTexCoord2f(0, 0);
				GL11.glVertex2f(0, 0);
				GL11.glTexCoord2f(1, 0);
				GL11.glVertex2f(1000, 0);
				GL11.glTexCoord2f(1, 1);
				GL11.glVertex2f(1000, 1000);
				GL11.glTexCoord2f(0, 1);
				GL11.glVertex2f(0, 1000);
				GL11.glEnd();
			}

			if (mouseX > 320 && mouseX < 704 && mouseY > 192 && mouseY <= 288) {

				Color.white.bind();
				menuBarIns.bind();

				GL11.glBegin(GL11.GL_QUADS);
				GL11.glTexCoord2f(0, 0);
				GL11.glVertex2f(0, 0);
				GL11.glTexCoord2f(1, 0);
				GL11.glVertex2f(1000, 0);
				GL11.glTexCoord2f(1, 1);
				GL11.glVertex2f(1000, 1000);
				GL11.glTexCoord2f(0, 1);
				GL11.glVertex2f(0, 1000);
				GL11.glEnd();
			}

			if (mouseX > 320 && mouseX < 704 && mouseY > 96 && mouseY <= 192) {

				Color.white.bind();
				menuBarOpt.bind();

				GL11.glBegin(GL11.GL_QUADS);
				GL11.glTexCoord2f(0, 0);
				GL11.glVertex2f(0, 0);
				GL11.glTexCoord2f(1, 0);
				GL11.glVertex2f(1000, 0);
				GL11.glTexCoord2f(1, 1);
				GL11.glVertex2f(1000, 1000);
				GL11.glTexCoord2f(0, 1);
				GL11.glVertex2f(0, 1000);
				GL11.glEnd();
			}

			if (mouseX > 320 && mouseX < 704 && mouseY > 0 && mouseY <= 96) {

				Color.white.bind();
				menuBarExt.bind();

				GL11.glBegin(GL11.GL_QUADS);
				GL11.glTexCoord2f(0, 0);
				GL11.glVertex2f(0, 0);
				GL11.glTexCoord2f(1, 0);
				GL11.glVertex2f(1000, 0);
				GL11.glTexCoord2f(1, 1);
				GL11.glVertex2f(1000, 1000);
				GL11.glTexCoord2f(0, 1);
				GL11.glVertex2f(0, 1000);
				GL11.glEnd();
			}

		} else {

			Color.white.bind();
			menuBar.bind();

			GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(0, 0);
			GL11.glVertex2f(0, 0);
			GL11.glTexCoord2f(1, 0);
			GL11.glVertex2f(1000, 0);
			GL11.glTexCoord2f(1, 1);
			GL11.glVertex2f(1000, 1000);
			GL11.glTexCoord2f(0, 1);
			GL11.glVertex2f(0, 1000);
			GL11.glEnd();
		}

		if (Mouse.getEventButtonState() && Mouse.getEventButton() == 0) {

			clickSound.playAsMusic(1.0f, 1.0f, false);

			if (mouseX > 320 && mouseX < 704 && mouseY > 384 && mouseY <= 480) {

				disp = false;
				tmpLevel = true;
				input = false;

				System.out.println("Load Game");
			}

			if (mouseX > 320 && mouseX < 704 && mouseY > 288 && mouseY <= 384) {

				disp = false;
				tmpLevel = true;
				input = false;

				System.out.println("New Game");
			}

			if (mouseX > 320 && mouseX < 704 && mouseY > 192 && mouseY <= 288) {

				disp = false;
				tmpLevel = true;
				input = false;

				System.out.println("Instructions");
			}

			if (mouseX > 320 && mouseX < 704 && mouseY > 96 && mouseY <= 192) {

				disp = false;
				tmpLevel = true;
				input = false;

				System.out.println("Options");
			}

			if (mouseX > 320 && mouseX < 704 && mouseY > 0 && mouseY <= 96) {
				System.exit(0);
			}
		}
	}
}
